﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StockFinder.Model;
using System.Net;
using System.IO;

namespace StockFinder.Services.Extractors.Yahoo
{
    public class YahooExchangeExtractor : IExchangeExtractor
    {
        private const string BASE_URL = "http://uk.finance.yahoo.com/q?s={0}";
        private const string NASDAQ = "NASDAQ";
        private const string NYSE = "NYSE";
        private const string AMEX = "AMEX";

        #region IExchangeExtractor Members

        public string GetExchange(Symbol symbol)
        {
            bool success = false;
            string exchange = string.Empty;

            string exchangeUrl = string.Format(BASE_URL, symbol.Name);

            //now get the spreadsheet
            HttpWebRequest webRequest = HttpWebRequest.Create(exchangeUrl) as HttpWebRequest;

            HttpWebResponse webResponse = null;

            try
            {
                webResponse = (HttpWebResponse)webRequest.GetResponse();

                success = true;
            }
            catch (WebException webException)
            {
                   
            }

            if (success)
            {
                string pageSource = null;

                //get the page source
                using (StreamReader streamReader = new StreamReader(webResponse.GetResponseStream()))
                {
                    pageSource = streamReader.ReadToEnd();
                }

                string symbolExchange = string.Format("<span>({0}: {1} )</span>", NASDAQ, symbol.Name);

                //check NASDAQ first
                if (pageSource.IndexOf(symbolExchange) > -1)
                {
                    exchange = NASDAQ;
                }
                else
                {
                    //check NYSE
                    symbolExchange = string.Format("<span>({0}: {1} )</span>", NYSE, symbol.Name);

                    if (pageSource.IndexOf(symbolExchange) > -1)
                    {
                        exchange = NYSE;
                    }
                    else
                    {
                        //check AMEX
                        symbolExchange = string.Format("<span>({0}: {1} )</span>", AMEX, symbol.Name);

                        if (pageSource.IndexOf(symbolExchange) > -1)
                        {
                            exchange = AMEX;
                        }
                        else
                        {
                            //check NasdaqGS
                            symbolExchange = string.Format("<span>(NasdaqGS: {0} )</span>", symbol.Name);

                            if (pageSource.IndexOf(symbolExchange) > -1)
                            {
                                exchange = NASDAQ;
                            }
                            else
                            {
                                //check NasdaqGS
                                symbolExchange = string.Format("<span>(NasdaqCM: {0} )</span>", symbol.Name);

                                if (pageSource.IndexOf(symbolExchange) > -1)
                                {
                                    exchange = NASDAQ;
                                }
                                else
                                {
                                    //check NasdaqGM
                                    symbolExchange = string.Format("<span>(NasdaqGM: {0} )</span>", symbol.Name);

                                    if (pageSource.IndexOf(symbolExchange) > -1)
                                    {
                                        exchange = NASDAQ;
                                    }
                                    else
                                    {
                                        //couldnt find
                                        throw new Exception(string.Format("Could not find Exchange for symbol '{0}'", symbol.Name));
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return exchange;
        }

        #endregion
    }
}
